package com.soundShape.utils
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.core.UIComponent;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	
	import spark.components.Image;
	import spark.effects.Fade;
	import spark.effects.Move;
	

	public class GreenArrow extends Image 
	{
		
		[Bindable]
		[Embed(source="assets/images/arrow.png")]
		private var arrow:Class;
		
		public static const POINT_LEFT:String = "pointLeft";
		public static const POINT_RIGHT:String = "pointRight";
		public static const POINT_UP:String = "pointUp";
		public static const POINT_DOWN:String = "pointDown";
		
		public var direction:String = POINT_RIGHT ;
		public var fadeDuration:int = 100;
		public var moveDuration:int = 1000;
		public var callback:Function;
		
		public var xFrom:int;
		public var xTo:int;
		
		public var yFrom:int;
		public var yTo:int;
		
		public function GreenArrow()
		{
			super();
			
			this.source = arrow;
			this.height = 50;
			this.alpha = 1.0;
			this.visible = false;
			this.id = "greenArrow";
			this.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
			this.addEventListener(MouseEvent.MOUSE_UP, handleMouse);
			this.addEventListener(FlexEvent.CREATION_COMPLETE, handleCreationComplete);
		}
		
		protected function handleCreationComplete(event:FlexEvent):void
		{
			switch(direction)
			{
				case POINT_LEFT:
					this.rotation = 180;
					break
				
				case POINT_RIGHT:
					this.rotation = 0;
					break
				
				case POINT_UP:
					this.rotation = -90;
					break
				
				case POINT_DOWN:
					this.rotation = 90;
					break
			}
			
		}
		
		public function doGreenArrow():void
		{
			
			this.visible = true;
			
			var move:Move = new Move();
			move.target = this;
			
			if(direction == POINT_LEFT || direction == POINT_RIGHT)
			{
				move.xFrom = xFrom;
				move.xTo = xTo;
			}
			
			if(direction == POINT_UP || direction == POINT_DOWN)
			{
				move.yFrom = yFrom;
				move.yTo = yTo;
			}
			
			move.duration = moveDuration;
			move.addEventListener(EffectEvent.EFFECT_END,doArrowEffectEnd);
			
			move.play();
		}
		
		public function doArrowEffectEnd(effectEvent:EffectEvent):void
		{
			var fade:Fade = new Fade();
			fade.target = this;
			fade.alphaFrom = 1.0;
			fade.alphaTo = 0.0;
			fade.duration = fadeDuration;
			fade.addEventListener(EffectEvent.EFFECT_END,doArrowEffectEndFade);
			fade.play();
		}
		
		public function doArrowEffectEndFade(effectEvent:EffectEvent):void
		{
			this.visible = false;
			this.alpha = 1.0;
			
			if(direction == POINT_LEFT || direction == POINT_RIGHT)
				this.x = xFrom;
			
			if(direction == POINT_UP || direction == POINT_DOWN)
				this.y = yFrom;
			
			if(this.callback != null)
				callback.call(null);
		}
		
		protected function handleMouse(mouseEvent:MouseEvent, rectangle:Rectangle=null):void
		{
			
			if(mouseEvent.type == MouseEvent.MOUSE_DOWN)
			{
				UIComponent(mouseEvent.currentTarget).startDrag(false,rectangle);
			}
			else if(mouseEvent.type == MouseEvent.MOUSE_UP)
			{
				UIComponent(mouseEvent.currentTarget).stopDrag();
				
				trace('{object:' + UIComponent(mouseEvent.currentTarget).id + ', ' +
					'x="' + UIComponent(mouseEvent.currentTarget).x + '" ' + 
					'y="' + UIComponent(mouseEvent.currentTarget).y  + '"}');
				
			}
		}
	}
}